package org.ihelpuoo.parser.sql;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

import org.ihelpuoo.db.sql.SQLUtils;
import org.ihelpuoo.lang.LoaderException;
import org.ihelpuoo.lang.RenderException;
import org.ihelpuoo.lang.StringUtils;
import org.ihelpuoo.parser.AbstractTextEngine;

public class FieldParser extends AbstractTextEngine implements IField
{
	private String		name;
	private String		defaultValue;
	private boolean		isAutoIncrement;
	private boolean		isNotNull;
	private boolean		isPrimaryKey;
	private boolean		isUnique;
	private FieldType	fieldType;
	private String		_s;

	public String getName()
	{
		return name;
	}

	void setPrimaryKey(boolean isPrimaryKey)
	{
		this.isPrimaryKey = isPrimaryKey;
	}

	void setUnique(boolean isUnique)
	{
		this.isUnique = isUnique;
	}

	public String getDefaultValue()
	{
		return defaultValue;
	}

	public boolean isAutoIncrement()
	{
		return isAutoIncrement;
	}

	public boolean isNotNull()
	{
		return isNotNull;
	}

	public boolean isPrimaryKey()
	{
		return isPrimaryKey;
	}

	public boolean isUnique()
	{
		return isUnique;
	}

	public void parse(Reader r) throws LoaderException
	{
		int c;
		try
		{
			StringBuffer sb = new StringBuffer();
			StringBuffer b = new StringBuffer();
			boolean isNotOn = false;
			while (true)
			{
				c = r.read();
				if (c != -1)
					b.append((char)c);
				if (c == (int)' ' || c == -1)
				{
					if (sb.length() == 0)
						if (c == -1)
							break;
						else
							continue;
					if (null == name)
					{
						name = SQLUtils.formatName(sb);
					}
					else if (null == fieldType)
					{
						fieldType = new FieldType(sb.toString());
					}
					else if ("unsigned".equalsIgnoreCase(sb.toString()))
					{
						fieldType.setUnsigned(true);
					}
					else if (!isNotOn && "NOT".equalsIgnoreCase(sb.toString()))
					{
						isNotOn = true;
					}
					else if (isNotOn && "NULL".equalsIgnoreCase(sb.toString()))
					{
						isNotOn = false;
						isNotNull = true;
					}
					else if ("auto_increment".equals(sb.toString()))
					{
						isAutoIncrement = true;
					}
					else if ("default".equalsIgnoreCase(sb.toString()))
					{
						int cc;
						StringBuffer v = new StringBuffer();
						while (-1 != (cc = r.read()))
						{
							b.append(cc);
							v.append((char)cc);
						}
						String vs = StringUtils.trim(v.toString());
						if ("NULL".equals(vs))
							defaultValue = null;
						else
						{
							if (vs.charAt(0) != '\'' || vs.charAt(vs.length() - 1) != '\'')
								defaultValue = vs;
							else
								defaultValue = vs.substring(1,vs.length() - 1);
						}
						break;
					}
					if (c == -1)
						break;
					sb = new StringBuffer();
				}
				else
				{
					sb.append((char)c);
				}
			}
			_s = b.toString();
		}
		catch (IOException e)
		{
			throw LoaderException.wrap(e);
		}
	}

	public void validate() throws LoaderException
	{
		if (false)
			throw new LoaderException("Syntax error on \"" + _s + "\"");
	}

	@Override
	public String toString()
	{
		return _s;
	}

	public void render2(Writer w) throws RenderException
	{
		try
		{
			w.append(_s);
		}
		catch (IOException e)
		{
			throw new RenderException(e);
		}
	}

	public FieldType getType()
	{
		return fieldType;
	}

}
